package com.fmi.timedealer.reporting;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fmi.timedealer.ActivityItem;
import com.fmi.timedealer.R;
import com.fmi.timedealer.datamanager.IDataManager;
import com.fmi.timedealer.datamanager.SQLiteDataManager;
import com.fmi.timedealer.reporting.model.CategoriesReportItem;
import com.fmi.timedealer.track.TrackLog;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class CategoriesReportActivity extends Activity{

	// data got by calling activity
	public static final String SELECTED_PERIOD = "SelectedPeriod";
	
	private IDataManager dbManager = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.pie_chart);
		
		dbManager = new SQLiteDataManager("MyDB", this);
		
		Bundle extras = getIntent().getExtras();
		String selectedTimePeriod = (String) extras.getSerializable(SELECTED_PERIOD);
		selectedTimePeriod = extras.getString(SELECTED_PERIOD);
		
		WebView googleChartView = new WebView(this);
		setContentView(googleChartView);
		String mUrl = getPieChartURL(selectedTimePeriod);
		googleChartView.loadUrl(mUrl);
	}
	
	private String getPieChartURL(String selectedPeriod) {
		
		Date startDate = new Date();
		Date endDate = new Date();
		if(selectedPeriod.equals(ReportsActivity.TIME_PERIOD_DAY)) {
			Calendar c = Calendar.getInstance();
			c.setTime(endDate);
			c.add(Calendar.DATE, -7);
			startDate.setTime(c.getTime().getTime());
		} else if(selectedPeriod.equals(ReportsActivity.TIME_PERIOD_MONTH)) {
			Calendar c = Calendar.getInstance();
			c.setTime(endDate);
			c.add(Calendar.DATE, -30);
			startDate.setTime(c.getTime().getTime());
		} else {
			startDate = null;
		}
		
		List<TrackLog> trackLog = dbManager.getAllTracks();
		
		List<CategoriesReportItem> l = new ArrayList<CategoriesReportItem>();
		
		double cTime;
		for(String m : dbManager.getCategoriesNames()) {
			cTime = 0;
			for(TrackLog i : trackLog) {
				if(m.equals(getCategoryByActivityID(i.getActivityId()))) {
					if(startDate == null) {
						cTime += i.getTimeSpent();
					} else if (i.getStartDate().compareTo(startDate) >= 0 &&
							i.getEndDate().compareTo(endDate) <= 0) {
						cTime += i.getTimeSpent();
					}
				}
			}
			l.add(new CategoriesReportItem(m, cTime));
		}
				
		int wholeTrackedTime = 0;
		List<String> items = new ArrayList<String>();
		
		for(CategoriesReportItem ri : l) {
			wholeTrackedTime += ri.getSpentTime();
			items.add(ri.getCategoryName());
		}
		
		Map<String, Double> activityToPercent = new HashMap<String, Double>();
		
		for(CategoriesReportItem r : l) {
			Double ddd = r.getSpentTime()/wholeTrackedTime*100;
	        DecimalFormat twoDForm = new DecimalFormat("#.##");
	        ddd = Double.valueOf(twoDForm.format(ddd));
			activityToPercent.put(r.getCategoryName(), new Double(ddd));
		}
		
		String data = "";
		String labels = "";
						
		for(String key : activityToPercent.keySet()) {
			data += (activityToPercent.get(key)+",");
			labels += (key+"|");
		}
		
		if(data.length() != 0 && labels.length() != 0) {
			data = String.copyValueOf(data.toCharArray(), 0, data.length()-1);
			labels = String.copyValueOf(labels.toCharArray(), 0, labels.length()-1);
		}

		String googleChartURL = "http://chart.apis.google.com/chart";
		String chartSize = "?chs=300x225";
		String chartType = "&cht=p3";
		String chartColors = "&chco=FFCC33,EE2C2C,9900CC,9AFF9A,0000FF,E7E643,33FF33";
		String chartData = "&chd=t:" + data;
		String chartDataLabels = "&chdl="+labels;
		String chartDataLabelsPosition = "&chdlp=b";
		String chartTitle = "&chtt=Report by categories for +"  + selectedPeriod.toLowerCase();
		
		String mUrl = googleChartURL+chartSize+chartType+chartColors+chartData+chartDataLabels+chartDataLabelsPosition+chartTitle;
	
		return mUrl;
	}

	private String getCategoryByActivityID(long activityId) {
		
		String category = "";
		List<ActivityItem> activities = dbManager.getAllActivities();
		for(ActivityItem i : activities) {
			if(i.getId() == activityId) {
				return i.getCategory();
			}
		}
		return category;
	}
}
